from django.shortcuts import render
from luffyapi.libs.geetest import GeetestLib
from rest_framework.response import Response
from .utils import get_user_by_account
from rest_framework import status as http_status
from rest_framework.views import APIView

# pc_geetest_id = "c28bbcc6e50daa037a426d9ba42c635c"
# pc_geetest_key = "291b8f8bf26ff2d53724a397c67c6304"
pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c"
pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4"


# pc_geetest_id = "fdecb42570289027518f89bffb714321"
# pc_geetest_key = "d2d951f8c52e5d2cffe43085ae58072b"

# Create your views here.
class CaptchaAPIView(APIView):

    def __init__(self):
        """验证码视图类"""
        self.status = False
        self.user_id = 0

    def get(self, request):
        """获取验证码"""
        print("获取验证码")
        print('<--------------------')
        username = request.query_params.get("username")
        user = get_user_by_account(username)
        print(user)
        if user is None:
            return Response({"message": "对不起，用户不存在！"}, status=http_status.HTTP_400_BAD_REQUEST)
        self.user_id = user.id
        print(self.user_id)
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        self.status = gt.pre_process(self.user_id)
        print(self.status)

        request.session[gt.GT_STATUS_SESSION_KEY] = self.status
        request.session["user_id"] = self.user_id
        # todo后面增加status和user_id保存到redis数据库中
        response_str = gt.get_response_str()
        print(response_str)
        # import pprint
        # print(pprint.pprint(request.session))
        print("self.status:", self.status)
        print('result：', response_str)

        return Response(response_str)

    def post(self, request):
        print("post-------------------->")
        """验证码的验证方法"""
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        print("gt: ", gt)

        challenge = request.POST.get(gt.FN_CHALLENGE, '')
        validate = request.POST.get(gt.FN_VALIDATE, '')
        seccode = request.POST.get(gt.FN_SECCODE, '')
        print("self.status:", self.status)

        if self.status:
            result = gt.success_validate(challenge, validate, seccode, self.user_id)
        else:
            result = gt.failback_validate(challenge, validate, seccode)

        # for key, value in request.session.items():
        #     print("开始循环session")
        #     print(f'{key}: {value}')

        # print(gt.GT_STATUS_SESSION_KEY)
        # print(request.session["user_id"])
        # status2 = request.session[gt.GT_STATUS_SESSION_KEY]
        # status2 = request.session["gt_server_status"]
        # user_id2 = request.session["user_id"]
        # print("status2, user_id2", status2, user_id2)
        # if status2:
        #     result = gt.success_validate(challenge, validate, seccode, user_id2)
        # else:
        #     result = gt.failback_validate(challenge, validate, seccode)

        print('result：', result)
        result = "<html><body><h1>登录成功</h1></body></html>" if result else "<html><body><h1>登录失败</h1></body></html>"
        print("post----------------------->")
        return Response(result)


from rest_framework.generics import CreateAPIView
from .models import User
from .serializers import UserModelSerializer


class UserAPIView(CreateAPIView):
    """用户信息视图"""
    queryset = User.objects.all()
    serializer_class = UserModelSerializer


"""
GET /user/mobile/<mobile>/
"""
import re
from rest_framework import status


class MobileAPIView(APIView):
    def get(self, request, mobile):
        ret = get_user_by_account(mobile)  # 查询用户注册数据库是否注册过
        if ret is not None:
            return Response({"message": "手机号已经被注册过！"}, status=status.HTTP_400_BAD_REQUEST)
        return Response({"message": "ok"})


import random
from django_redis import get_redis_connection
from luffyapi.settings import constants
from luffyapi.libs.yuntongxun.sms import CCP

import logging

log = logging.getLogger("django")


class SMSAPIView(APIView):
    def get(self, request, mobile):
        """短信发送"""
        # print(request)
        # print(mobile)

        # 1. 判断手机号码是否在60秒内曾经发送过短信
        redis_conn = get_redis_connection("sms_code")
        ret = redis_conn.get("mobile_%s" % mobile)
        if ret is not None:
            return Response({"message": "对不起，60秒内已经发送过短信，请耐心等待"}, status=status.HTTP_400_BAD_REQUEST)

        # 2. 生成短信验证码
        # sms_code = "%06d" % random.randint(1, 999999)
        sms_code = "%04d" % random.randint(1, 9999)
        # print(sms_code)

        # 3. 保存短信验证码到redis[使用事务把多条命令集中发送给redis]
        # 创建管道对象
        pipe = redis_conn.pipeline()
        # 开启事务【无法管理数据库的读取数据操作】
        pipe.multi()
        # 把事务中要完成的所有操作，写入到管道中
        pipe.setex("sms_%s" % mobile, constants.SMS_EXPIRE_TIME, sms_code)
        pipe.setex("mobile_%s" % mobile, constants.SMS_INTERVAL_TIME, "_")
        # 执行事务
        pipe.execute()

        # 4. 调用短信sdk，发送短信
        try:
            from mycelery.sms.tasks import send_sms
            send_sms.delay(mobile, sms_code)
            # send_sms.apply_async(args=[mobile, sms_code], queue='celery')  # 指定队列

            # ccp = CCP()
            # ret = ccp.send_template_sms(mobile, [sms_code, constants.SMS_EXPIRE_TIME // 60], constants.SMS_TEMPLATE_ID)
            # print(ret)
            # if not ret:
            #     log.error("用户注册短信发送失败！手机号：%s" % mobile)
            #     return Response({"message": "发送短信失败！"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        except:
            return Response({"message": "发送短信失败！"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 5. 响应发送短信的结果
        return Response({"message": "发送短信成功！"})


from rest_framework.generics import ListAPIView
from order.models import Order
from .serializers import UserOrderModelSerializer
from rest_framework.permissions import IsAuthenticated


class UserOrderAPIView(ListAPIView):
    # queryset = Order.objects.all()
    serializer_class = UserOrderModelSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        # print(self.request.user.id)
        return Order.objects.filter(user_id=self.request.user.id)
